#include <iostream>
#include "vector"
#include "algorithm"

using namespace std;

/*
 * 谓词
 *      返回bool类型的仿函数称为谓词
 *      如果operator() 接受一个参数，一元谓词
 *      如果operator() 接受两个参数，二元谓词
 */

// 一元谓词
class MyFunctionObject{
public:
    bool operator()(int value){
        return value > 5;
    }
};

// 二元谓词
class MyCompare{
public:
    bool operator()(int value1, int value2){
        return value1 > value2;
    }
};

void test01(){
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    // 找到返回迭代器的位置，找不到返回终止迭代器的位置
    vector<int>::iterator it = find_if(v.begin(), v.end(), MyFunctionObject());
    if(it == v.end()){
        cout << "未找到" << endl;
    } else{
        cout << "找到数字：" << *it << endl;
    }
}

void test02(){
    vector<int> v;
    v.push_back(40);
    v.push_back(10);
    v.push_back(70);
    v.push_back(20);
    v.push_back(50);
    for (vector<int>::iterator it= v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    sort(v.begin(), v.end());
    cout << "使用默认从小到大排序后"  << endl;
    for (vector<int>::iterator it= v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    cout << "使用自定义从大到小排序后"  << endl;
    sort(v.begin(), v.end(), MyCompare());
    for (vector<int>::iterator it= v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

int main() {

    test01();
    test02();

    return 0;
}
